(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("fxRateService", fxRateService);
	fxRateService.$inject = [ '$q', 'loggerFactory', 'ajaxServiceFactory', 'modalService' ];
	function fxRateService($q, loggerFactory, ajaxServiceFactory, modalService) {
		var log = loggerFactory.getLogger("fxRateService");
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getCurrencyOptions : {
				url : 'do/cda/dataList/currencies',
				method : 'get'
			},
			getBaseCcy : {
				url : 'do/cda/site/baseCcyByCurrentSiteCode',
				method : 'get'
			},
			getSiteInfor : {
				url : 'do/cda/site/getSiteInfor',
				method : 'get'
			}
		});
		
		function getSiteInfor() {
			var defer = $q.defer();
			ajaxService.getSiteInfor().then(function(response) {
				defer.resolve(response ? response.data : {});
			});
			return defer.promise;
		};
		function getCurrencyOptions() {
			var defer = $q.defer();
			ajaxService.getCurrencyOptions().then(function(response) {
				defer.resolve(response ? response : {});
			});
			return defer.promise;
		};
		
		function getBaseCcy() {
			var defer = $q.defer();
			ajaxService.getBaseCcy().then(function(response) {
				defer.resolve(response ? response.data : "");
			});
			return defer.promise;
		};
		
		function initOptions(){
			var defer1 = $q.defer();
			var defer2 = $q.defer();
			getCurrencyOptions().then(function(response) {
				defer1.resolve(response);
			}, angular.noop);
			getBaseCcy().then(function(response) {
				defer2.resolve(response);
			}, angular.noop);
			return $q.all({
				currencyOptions : defer1.promise,
				baseCcy : defer2.promise
			});
		};
		
		function fxRateHistoryLookup(selectedRows){
			if (!selectedRows || selectedRows.length != 1) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			modalService.openCustomDialogs({
				'dialogsModule' : 'cda.fxRateHistory',
				'size' : [1300,400],
				'params':{
					'selectedRows' : selectedRows,
				}
			});
		};
		
		return {initOptions : initOptions,
			fxRateHistoryLookup : fxRateHistoryLookup,
			getSiteInfor : getSiteInfor};
	}
})(angular);